/**
 * 
 */
package com.gosophia.fundationInfo.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gosophia.userManagement.dto.UserAccountDTO;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.entity.UserAccount;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;
import com.gosophia.userManagement.ws.UserManagementWebService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 功能：      
 * @author zhangdongsheng
 * @since  2010-12-9
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Scope("prototype")
@Controller("LoginAction")
public class LoginAction  extends ActionSupport {

	private static final long serialVersionUID = 4820001199243590153L;

	private String loginName;//登陆账号名称
    
    private String password;//登陆密码
    
    private String errorMsg;
    
    @Autowired
    private UserManagementWebService userManagementWebService;
    
    /*  
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    @Override
    public String execute() throws Exception {
        
        //验证用户是否存在
    	UserAccountDTO userAccount = validUserExist();
    	if(userAccount == null){
    		errorMsg = "用户账号不存在";
    		return "ERROR";
    	}else if(!validUserPassword(userAccount,password)){
    		errorMsg = "密码不正确";
    		return "ERROR";
    	}else{
			ActionContext.getContext().getSession().put("userAccountId", //用户账号Id
					userAccount.getUserAccountId());
			ActionContext.getContext().getSession().put("userAccountName", //用户账号名称（登录名称）
					userAccount.getUserAccountName());
			UserInfoDTO userInfo = userAccount.getUserInfoDTO();
			ActionContext.getContext().getSession().put("userInfoId", userInfo.getUserInfoId()); //用户详细信息Id
			ActionContext.getContext().getSession().put("userName", userInfo.getUserName()); //用户姓名
    		return SUCCESS;
    	}
        //验证用户密码是否正确
        
        //写入session ，用户 userAccount   userInfo 
        
        //ActionContext.getContext().getSession()
        
    }

    
    private UserAccountDTO validUserExist(){
        return userManagementWebService.findUserAccountByName(loginName);
    }
    
    private boolean validUserPassword(UserAccountDTO userAccount, String password){
    	if(userAccount.getUserAccountPass().equals(password)){
    		return true;
    	}
        return  false;
    }
    
    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}


	public String getErrorMsg() {
		return errorMsg;
	}


  
}
